import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/home.vue'
import Login from '../components/Login.vue'
import Welcome from '../components/welcome.vue'
import bookList from '../components/bookList.vue'
import jisuanji from '../components/book/jisuanji.vue'
import wenxue from '../components/book/wenxue.vue'
import waiyu from '../components/book/waiyu.vue'
import falv from '../components/book/falv.vue'
import yishu from '../components/book/yishu.vue'
import kaoshi from '../components/book/kaoshi.vue'
import jianzhu from '../components/book/jianzhu.vue'
import jingguan from '../components/book/jingguan.vue'


Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
  { path: "/",redirect:"/Login"},
  { path: "/Login", component: Login},
  { 
	  path: "/Home",
	  component: Home,
		redirect: '/welcome',
	  children: [{path: '/welcome',component: Welcome},{path: '/bookList',component: bookList},
				 {path: '/jisuanji',component: jisuanji}, {path: '/yishu',component: yishu},	
				 {path: '/wenxue',component: wenxue},{path: '/kaoshi',component: kaoshi},
				 {path: '/waiyu',component: waiyu},{path: '/jianzhu',component: jianzhu},
				 {path: '/falv',component: falv},{path: '/jingguan',component: jingguan}]
	}
  ]
})

router.beforeEach((to,from, next) =>{
	//如果访问登录页面直接放行
	if(to.path === '/Login') return next()
	// 从sessionStorage 中获取到 token 
	const tokenStr = window.sessionStorage.getItem('token')
	//没有token,强制跳转到登录页
	if (!tokenStr) return next('/Login')
	next()
})

export default router
